#include "text1.h"

text1::text1()
{

}

void text1::addX(double value)
{
    x.push_back(value);
}

void text1::addY(double value)
{
    y.push_back(value);
}

void text1::apply(int n, double x)
{
    if(Lbase.size()>0)
        Lbase.clear();
    for (int i = 0; i <= n; ++i) {
        Lagrange_base(i,n,x);
    }
}

double text1::getLagrange_base(int i)
{
    return Lbase[i];
}

void text1::Lagrange_base(int i, int n, double x)
{
    double L = 1.0;
    for(int j=0;j<=n;++j)
    {
        if(j==i)
            continue;
        double lj;
        lj = (x-this->x[j])/(this->x[i]-this->x[j]);
        L*=lj;
        qDebug()<<"lbase for"<<j<<":"<<lj;
    }
    qDebug()<<"lbase"<<L;
    Lbase.push_back(L);
}

double text1::getResult()
{
    double result = 0;
    for (int i = 0; i < Lbase.size(); ++i) {
        result+=Lbase[i]*y[i];
    }
    return result;
}

void text1::clear()
{
    x.clear();
    y.clear();
    Lbase.clear();
}
